package com.yyg.boot.config;

import com.yyg.boot.service.MyService;
import com.yyg.boot.service.impl.MyServiceImpl;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

/**
 * @Author 一一哥Sun
 * @Date Created in 2020/5/9
 * @Description Description
 */
@Configuration
public class CxfConfig {

    //注意:该方法的名称不能使用dispatcherServlet(),否则会导致ErrorMvcAutoConfiguration错误.
    //org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration
    //required a bean of type 'org.springframework.boot.autoconfigure.web.servlet.DispatcherServletPath'
    // that could not be found.
//    @Bean
//    public ServletRegistrationBean dispatcherServlet() {
//        return new ServletRegistrationBean(new CXFServlet(), "/myService/*");
//    }

    @Bean
    public ServletRegistrationBean createServletRegistrationBean() {
        return new ServletRegistrationBean(new CXFServlet(), "/myService/*");
    }

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

    @Bean
    public MyService myService() {
        return new MyServiceImpl();
    }

    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), myService());
        endpoint.publish("/api");
        return endpoint;
    }

}
